package com.rjwh.dingdong.client.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.rjwh.dingdong.client.bean.localbean.PlayList;
import com.rjwh.dingdong.client.bean.localbean.Song;
import com.rjwh.dingdong.client.player.IPlayback;
import com.rjwh.dingdong.client.player.PlayConstants;
import com.rjwh.dingdong.client.player.PlayMode;
import com.rjwh.dingdong.client.player.PlayState;
import com.rjwh.dingdong.client.player.Player;

/**
 * Created with Android Studio. User: ryan.hoo.j@gmail.com Date: 9/12/16 Time:
 * 4:27 PM Desc: PlayService
 */
public class PlaybackService extends Service implements IPlayback {


	private Player mPlayer;


	private TelephonyManager mTelephonyManager;
	private PhoneStateListener mPhoneStateListener;

	private final Binder mBinder = new LocalBinder();

	public class LocalBinder extends Binder {
		public PlaybackService getService() {
			return PlaybackService.this;
		}
	}

	@Override
	public void onCreate() {
		super.onCreate();
		mPlayer = Player.getInstance(this);
		mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
		mPhoneStateListener = new PhoneStateListener() {
			private boolean isPause;
			@Override
			public void onCallStateChanged(int state, String incomingNumber) {
				//挂断
				if (state == TelephonyManager.CALL_STATE_IDLE) {
					Log.i("HTML","挂断---- 继续播放"+isPause);
					if(isPause)mPlayer.play();
				} else {
					//来电
					if (mPlayer != null) {
						isPause = mPlayer.pause();
						Log.i("HTML","接听 暂停中。。。");
					}
				}
			}
		};
		mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (intent != null) {
			String action = intent.getAction();
			if (PlayConstants.ACTION_PLAY_TOGGLE.equals(action)) {
				if (isPlaying()) {
					pause();
				} else {
					play();
				}
			} else if (PlayConstants.ACTION_PLAY_NEXT.equals(action)) {
				playNext();
			} else if (PlayConstants.ACTION_PLAY_LAST.equals(action)) {
				playLast();
			} else if (PlayConstants.ACTION_STOP_SERVICE.equals(action)) {
				if (isPlaying()) {
					pause();
				}
				stopForeground(true);
			} else if (PlayConstants.ACTION_SEEK_TO.equals(action)) {
				int duration = intent.getIntExtra("prog", 0);
				seekTo(duration);
			}
		}
		return START_STICKY;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	@Override
	public boolean stopService(Intent name) {
		stopForeground(true);
		return super.stopService(name);
	}

	@Override
	public void onDestroy() {
		releasePlayer();
		super.onDestroy();
	}

	@Override
	public void setPlayList(PlayList list) {
		mPlayer.setPlayList(list);
	}

	@Override
	public PlayList getPlayList() {
		return mPlayer.getPlayList();
	}

	@Override
	public boolean play() {

		return mPlayer.play();
	}

	@Override
	public boolean play(PlayList list) {
		return mPlayer.play(list);
	}

	@Override
	public boolean play(PlayList list, int startIndex) {
		return mPlayer.play(list, startIndex);
	}

	@Override
	public boolean play(Song song) {
		return mPlayer.play(song);
	}

	@Override
	public boolean playLast() {
		return mPlayer.playLast();
	}

	@Override
	public boolean playNext() {
		return mPlayer.playNext();
	}

	@Override
	public boolean pause() {
		return mPlayer.pause();
	}

	@Override
	public boolean stop() {
		return mPlayer.stop();
	}

	@Override
	public boolean isPlaying() {
		return mPlayer.isPlaying();
	}

	@Override
	public int getProgress() {
		return mPlayer.getProgress();
	}

	@Override
	public Song getPlayingSong() {
		return mPlayer.getPlayingSong();
	}

	@Override
	public boolean seekTo(int progress) {
		return mPlayer.seekTo(progress);
	}

	@Override
	public void setPlayMode(PlayMode playMode) {
		mPlayer.setPlayMode(playMode);
	}


	@Override
	public void releasePlayer() {
		mPlayer.releasePlayer();
		super.onDestroy();
	}
	public int getDuration()
	{
		return mPlayer.getDuration();
	}


	@Override
	public PlayState getPlayStatus() {
		// TODO Auto-generated method stub
		return mPlayer.getPlayStatus();
	}

}
